package indigo.examples;

import javax.servlet.Filter;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.realm.text.TextConfigurationRealm;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;

import indigo.captcha.config.EnableImageCaptcha;
import indigo.core.config.EnableSqlEntity;
import indigo.shiro.config.EnableShiroAnnotationsAuthorization;
import indigo.shiro.config.EnableShiroFilter;
import indigo.shiro.config.EnableShiroFilter.FilterChain;
import indigo.ueditor.config.EnableUEditor;

@SpringBootApplication
@EnableShiroFilter(@FilterChain(name = "/*", definition = "anon"))
@EnableShiroAnnotationsAuthorization
@EnableSqlEntity("indigo.examples.entity")
@EnableImageCaptcha("/captcha")
@EnableUEditor("d:\\indigo_ueditor_repo")
public class Application {
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

	@Bean
	public FilterRegistrationBean shiroFilterRegistration(
			@Qualifier("shiroFilter") Filter shiroFilter) {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		registration.setName("shiroFilter");
		registration.setFilter(shiroFilter);
		return registration;
	}

	@Bean
	public SecurityManager securityManager(Realm realm) {
		DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
		manager.setRealm(realm);
		return manager;
	}

	@Bean
	public Realm realm() {
		String[] roleDefs = {
				"root = *"
		};
		String[] userDefs = {
				"admin = 123456, root"
		};
		TextConfigurationRealm realm = new TextConfigurationRealm();
		realm.setRoleDefinitions(String.join(",", roleDefs));
		realm.setUserDefinitions(String.join(",", userDefs));
		return realm;
	}
}
